var login = "UserName:<input type=\"text\" name=\"user.user_name\" id=\"userName\"><br>"
		+ "UserPwd&nbsp;:<input type=\"password\" name=\"user.user_pwd\" id=\"userPwd\"><br>"
		+ "<input type=\"button\" id=\"loginUser\" value=\"Login\">";
function loginInfo(username) {
	return "<span><h1>Welcome!</h1></span><br />"
			+ "<h3>"
			+ username
			+ "<a id=\"logOff\" href=\"#\" onclick=\"return false;\">Log Off</a>"
			+ "</h3>";
}
function onbeforeonload() {
	var n = window.event.screenX - window.screenLeft;
	var b = n > document.documentElement.scrollWidth - 20;
	if (b && window.event.clientY < 0 || window.event.altKey) {
		window.open("http://localhost:8080/MineDemo/user/user_closeIE");
	}
};
$(function() {
	$.post("login/login_checkSession", {
		"checkSession" : "1"
	}, function(data) {
		if (data.error != "error") {
			$("#loginInfo").html(loginInfo(data.user_name));
		}
	});
	$("#readJson").click(function() {
		$.post("user/user_readJson", function(data) {
			if (data.error == "error") {
				alert(data.msg);
			} else {
				$("#thisJson").append(data);
			}
		});
	});
	$("#showAllUser").click(
			function() {
				var div = "";
				$.post("user/user_findAllUser", function(data) {
					if (data.error == "error") {
						alert(data.msg);
					} else {
						$.each(data, function() {
							div += "<ul>" + "<li>user_name:" + this.user_name
									+ "</li>" + "<li>user_pwd&nbsp;:"
									+ this.user_pwd + "</li>" + "</ul>";
						});
						$("#showUsersDiv").html(div);
						$("li").each(function(index) {
							if (index == 0) {
								$(this).css("border-bottom", "none");
							}
							if (index % 2 == 0) {
								$(this).css("border-bottom", "none");
							} else {
								$(this).css("border-top", "none");
							}
						});
					}
				});
			});
	$(document).on("click", "#loginUser", function() {
		$.post("login/login_loginUser", {
			"user.user_name" : $("#userName").val(),
			"user.user_pwd" : $("#userPwd").val()
		}, function(data) {
			if (data.error != "error" && data.error != "logged") {
				$("#loginInfo").html(loginInfo(data.user_name));
			} else if (data.error == "error") {
				alert("Sorry! Not find the user!");
			} else if (data.error == "logged") {
				alert("Sorry! This user has logged on!");
			}
		});
	});
	$(document).on("click", "#logOff", function() {
		$.post("user/user_userLogoff", function(data) {
				$("#loginInfo").html(login);
		});
	});
});




